home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / CUGUK / COMMS / C101.ZIP / UUPC11XS.ZIP / UUCICO / SCRIPT.C < prev    next >
C/C++ Source or Header  |  1992-11-12  |  14KB  |  439 lines

  1. /*--------------------------------------------------------------------*/
  2. /*    s c r i p t . c                                                 */
  3. /*                                                                    */
  4. /*    Script processing routines for UUPC/extended                    */
  5. /*                                                                    */
  6. /*    John H. DuBois III  3/31/90                                     */
  7. /*--------------------------------------------------------------------*/
  8.  
  9. /*--------------------------------------------------------------------*/
  10. /*                        System include files                        */
  11. /*--------------------------------------------------------------------*/
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <ctype.h>
  16. #include <string.h>
  17. #include <time.h>
  18.  
  19. /*--------------------------------------------------------------------*/
  20. /*                    UUPC/extended include files                     */
  21. /*--------------------------------------------------------------------*/
  22.  
  23. #include "lib.h"
  24. #include "dcp.h"
  25. #include "dcpsys.h"
  26. #include "hostable.h"
  27. #include "hlib.h"
  28. #include "modem.h"
  29. #include "script.h"
  30. #include "security.h"
  31. #include "ssleep.h"
  32. #include "catcher.h"
  33. #include "ulib.h"
  34.  
  35. /*--------------------------------------------------------------------*/
  36. /*                           Local defines                            */
  37. /*--------------------------------------------------------------------*/
  38.  
  39. #define MAXMATCH 64              /* max length of search string; must
  40.                                     be a power of 2                  */
  41. #define QINDMASK (MAXMATCH - 1)  /* bit mask to get queue index      */
  42.  
  43. #define EOTMSG "\004\r\004\r"
  44.  
  45. /*--------------------------------------------------------------------*/
  46. /*                    Internal function prototypes                    */
  47. /*--------------------------------------------------------------------*/
  48.  
  49. static int StrMatch(char *MatchStr, char C, char **failure);
  50.                                  /* Internal match routine           */
  51.  
  52. static boolean Match( char *Search,
  53.                       char *Buffer,
  54.                       size_t *SearchPos);
  55.  
  56. static size_t MatchInit( const char *MatchStr );
  57.  
  58. static boolean writestr(register char *s);
  59.  
  60. /*--------------------------------------------------------------------*/
  61. /*                          Global variables                          */
  62. /*--------------------------------------------------------------------*/
  63.  
  64. currentfile();
  65.  
  66. /*
  67.  *       e x p e c t s t r
  68.  *
  69.  *       wait for a pattern on input
  70.  *
  71.  *
  72.  *       expectstr reads characters from input using sread, and
  73.  *       compares them to a search string.  It reads characters until
  74.  *       either the search string has been seen on the input or a
  75.  *       specified timeout interval has passed without any characters
  76.  *       being read.
  77.  *
  78.  *      Global variables: none.
  79.  *
  80.  *      Input parameters:
  81.  *      Search is the string that is searched for on the input.
  82.  *      Timeout is the timeout interval passed to sread.
  83.  *
  84.  *      Output parameters: none.
  85.  *
  86.  *      Return value:
  87.  *      TRUE is returned if the search string is found on input.
  88.  *      FALSE is returned if sread times out.
  89.  */
  90.  
  91. int expectstr(char *Search, unsigned int Timeout, char **failure)
  92. {
  93.    char buf[BUFSIZ];
  94.    int result;
  95.    time_t quit = time( NULL ) + Timeout;
  96.    register char *ptr = buf;
  97.  
  98.    printmsg(2, "wanted \"%s\"", Search);
  99.  
  100.    if (!strlen(Search))                      /* expects nothing */
  101.        return TRUE;
  102.  
  103.    StrMatch(Search,'\0', failure);    /* set up search string */
  104.  
  105.    do {
  106.       if (ptr == &buf[BUFSIZ-1])
  107.         ptr = buf;          /* Save last character for term \0  */
  108.  
  109.       if (sread(ptr , 1, (int) (quit - time(NULL))) < 1)
  110.       {
  111.                               /* The scan failed?                    */
  112.          char *s;
  113.  
  114.          if ( terminate_processing )
  115.             return 0;
  116.  
  117.          while ( ptr > buf )
  118.             if (*(--ptr) > ' ')
  119.                break;    /* Locate the last printable char      */
  120.  
  121.          *(ptr+1) = '\0';   /* Terminate the string             */
  122.  
  123.          for ( s = buf; (*s > '\0') && (*s <= ' '); s++ );
  124.                             /* Locate the first printable char  */
  125.  
  126.          while ( ptr-- > s )/* Zap control chars                */
  127.             if (*ptr < ' ')
  128.                *ptr = '?';
  129.  
  130.          if ( debuglevel < 2 )
  131.             printmsg(1, "wanted \"%s\"", Search);
  132.  
  133.          printmsg(1, "got ??? \"%s\"",s );
  134.          return FALSE;
  135.  
  136.       } /* if (sread(ptr , 1, Timeout) < 1) */
  137.  
  138.       *ptr &= 0x7f;
  139.  
  140.       result = StrMatch(Search, *ptr++, failure);
  141.    } while (!result);
  142.  
  143.    return result;
  144.  
  145. } /*expectstr*/
  146.  
  147.  
  148. /*
  149.  *      StrMatch: Incrementally search for a string.
  150.  *      John H. DuBois III  3/31/90
  151.  *      StrMatch searches for a string in a sequence of characters.
  152.  *      The string to search for is passed in an initial setup call
  153.  *      (input character in this call is \0)
  154.  *      Further calls with the search string pass one
  155.  *      character per call.
  156.  *      The characters are built up into an input string.
  157.  *      After each character is added to the input string,
  158.  *      the search string is compared to the last length(search string)
  159.  *      characters of the input string to determine whether the search
  160.  *      string has been found.
  161.  *
  162.  *      Global variables: none.
  163.  *
  164.  *      Input parameters:
  165.  *      MatchStr is the string to search for.
  166.  *      C is the character to add to the input string.
  167.  *      It is ignored on a setup call.
  168.  *
  169.  *      Output parameters: None.
  170.  *
  171.  *      Return value:
  172.  *      On the setup call, -1 is returned if the search string is
  173.  *      longer than the input string buffer.  Otherwise, 0 is returned.
  174.  *
  175.  *      On comparison calls,
  176.  *          1 is returned if the search string has been found.
  177.  *          > 1 is returned if a failure string has been found.
  178.  *          Otherwise 0 is returned.
  179.  */
  180.  
  181.  
  182. static int StrMatch(char *MatchStr, char C, char **failure)
  183. {
  184. /*
  185.  *      The input string is stored in a circular buffer of MAXMATCH
  186.  *      characters.  If the search string is found in the input,
  187.  *      then the last character added to the buffer will be the last
  188.  *      character of the search string.  Therefore, the string
  189.  *      compare will always start SearchLen characters behind the
  190.  *      position where characters are added to the buffer.
  191.  */
  192.  
  193.    static char Buffer[MAXMATCH];       /* Input string buffer */
  194.    static size_t PutPos;               /* Where to add chars to buffer */
  195.  
  196.    static size_t SearchPos[MAXLIST];
  197.    static size_t SearchPosition;
  198.                                        /* Buffer loc to start compare */
  199.    static size_t alternates;
  200.    size_t subscript;
  201.  
  202. /*--------------------------------------------------------------------*/
  203. /*                       Handle initialize call                       */
  204. /*--------------------------------------------------------------------*/
  205.  
  206.    if ( C == '\0')
  207.    {                                   /* Set up call */
  208.       memset(Buffer,'\0',MAXMATCH);    /* Clear buffer */
  209.       PutPos = 0;
  210.  
  211.       SearchPosition = MatchInit( MatchStr );
  212.  
  213.       alternates = 0;
  214.       if ( failure != NULL )
  215.       {
  216.          while (failure[alternates] != NULL )
  217.          {
  218.             SearchPos[alternates] = MatchInit( failure[alternates] );
  219.             alternates++;
  220.          } /* while (failure[alternates] != NULL ) */
  221.  
  222.       } /* if ( failure != NULL ) */
  223.  
  224.       return 0;
  225.    } /* if (MatchStr) */
  226.  
  227. /*--------------------------------------------------------------------*/
  228. /*                       Look for primary match                       */
  229. /*--------------------------------------------------------------------*/
  230.  
  231.    Buffer[ PutPos++ & QINDMASK] = C;
  232.  
  233.    if (Match( MatchStr, Buffer, &SearchPosition))
  234.    {
  235.       printmsg(2, "got that");
  236.       return 1;
  237.    }
  238.  
  239. /*--------------------------------------------------------------------*/
  240. /*                     Look for alternate matches                     */
  241. /*--------------------------------------------------------------------*/
  242.  
  243.    if ( alternates > 0 )
  244.    {
  245.       subscript = alternates;
  246.  
  247.       while ( subscript-- )
  248.       {
  249.          if (Match( failure[subscript], Buffer, &SearchPos[subscript]))
  250.          {
  251.             printmsg(0,"got \"%s\" (failure)",failure[subscript]);
  252.             return 2;
  253.          }
  254.       } /* while ( subscript-- ) */
  255.  
  256.    } /* if ( alternates > 0 ) */
  257.  
  258.    return 0;
  259.  
  260. } /* StrMatch */
  261.  
  262. /*--------------------------------------------------------------------*/
  263. /*    m a t c h                                                       */
  264. /*                                                                    */
  265. /*    Match a single string                                           */
  266. /*--------------------------------------------------------------------*/
  267.  
  268. static boolean Match( char *Search,
  269.                       char *Buffer,
  270.                       size_t *SearchPos)
  271. {
  272.    int BufInd;             /* Index to input string buffer for string
  273.                               compare */
  274.    char *SearchInd;        /* Index to search string for string
  275.                               compare */
  276.  
  277.    *SearchPos += 1;
  278.    for (BufInd = *SearchPos, SearchInd = Search; *SearchInd; SearchInd++)
  279.    {
  280.      if (Buffer[BufInd++ & QINDMASK] != *SearchInd)
  281.         return FALSE;
  282.    }
  283.  
  284.    return TRUE;
  285.  
  286. } /* Match */
  287.  
  288. /*--------------------------------------------------------------------*/
  289. /*    M a t c h I n i t                                               */
  290. /*                                                                    */
  291. /*    Initialize one set of parameters for MATCH                      */
  292. /*--------------------------------------------------------------------*/
  293.  
  294. static size_t MatchInit( const char *MatchStr )
  295. {
  296.    size_t SearchLen = strlen(MatchStr);
  297.  
  298.    if (SearchLen > MAXMATCH)
  299.    {
  300.       printmsg(0,"StrMatch: String to match '%s' is too long.\n",
  301.            MatchStr);
  302.       panic();
  303.    }
  304.  
  305.    return MAXMATCH - SearchLen;
  306.  
  307. } /* MatchInit */
  308.  
  309. /*--------------------------------------------------------------------*/
  310. /*    w r i t e s t r                                                 */
  311. /*                                                                    */
  312. /*    Send a string to the port during login                          */
  313. /*--------------------------------------------------------------------*/
  314.  
  315. static boolean writestr(register char *s)
  316. {
  317.    register char last = '\0';
  318.    boolean nocr  = FALSE;
  319.    unsigned char digit;
  320.  
  321.    if equal(s,"BREAK")
  322.    {
  323.       ssendbrk(0);
  324.       return TRUE;               /* Don't bother with a CR after this   */
  325.    }
  326.    while (*s) {
  327.       if (last == '\\') {
  328.          last = *s;
  329.          switch (*s) {
  330.          case 'd':   /* delay */
  331.          case 'D':
  332.             ssleep(2);
  333.             break;
  334.          case 'c':   /* don't output CR at end of string */
  335.          case 'C':
  336.             nocr = TRUE;
  337.             break;
  338.          case 'r':   /* carriage return */
  339.          case 'R':
  340.          case 'm':
  341.          case 'M':
  342.             slowwrite("\r", 1);
  343.             break;
  344.          case 'n':   /* new line */
  345.          case 'N':
  346.             slowwrite("\n", 1);
  347.             break;
  348.          case 'p':   /* delay */
  349.          case 'P':
  350.             ddelay(400);
  351.             break;
  352.          case 'b':   /* backspace */
  353.          case 'B':
  354.             slowwrite("\b", 1);
  355.             break;
  356.          case 't':   /* tab */
  357.          case 'T':
  358.             slowwrite("\t", 1);
  359.             break;
  360.          case 's':   /* space */
  361.          case 'S':
  362.             slowwrite(" ", 1);
  363.             break;
  364.          case 'z':   /* set serial port speed */
  365.          case 'Z':
  366.             SIOSpeed(atoi(++s));
  367.             while (isdigit(*(s+1)))
  368.                s++;
  369.             break;
  370.  
  371.          case '0':
  372.          case '1':
  373.          case '2':
  374.          case '3':
  375.          case '4':
  376.          case '5':
  377.          case '6':
  378.          case '7':
  379.             digit = 0;
  380.             while( (*s >= '0') && (*s < '8'))
  381.                digit = (unsigned char) (digit * 8 + *s++ - '0');
  382.             s--;              /* Backup before non-numeric char      */
  383.             slowwrite((char *) &digit,1);
  384.             break;
  385.  
  386.          default: /* ordinary character */
  387.             slowwrite(s, 1);
  388.             last = '\0';      /* Zap any repeated backslash (\)      */
  389.          }
  390.       }
  391.       else if (*s != '\\') /* backslash */
  392.          slowwrite(s, 1);
  393.       else
  394.          last = *s;
  395.       s++;
  396.    }
  397.  
  398.    return nocr;
  399.  
  400. } /*writestr*/
  401.  
  402.  
  403. /*
  404.    s e n d s t r
  405.  
  406.    Send line of login sequence
  407. */
  408.  
  409. void sendstr(char *str)
  410. {
  411.    printmsg(2, "sending \"%s\"", str);
  412.  
  413.    if (equaln(str, "BREAK", 5)) {
  414.       int   nulls;
  415.       nulls = atoi(&str[5]);
  416.       if (nulls <= 0 || nulls > 10)
  417.          nulls = 3;
  418.       ssendbrk(nulls);  /* send a break signal */
  419.       return;
  420.    }
  421.  
  422.    if (equal(str, "EOT")) {
  423.       slowwrite(EOTMSG, strlen(EOTMSG));
  424.       return;
  425.    }
  426.  
  427.    if (equal(str, "\"\""))
  428.       *str = '\0';
  429.  
  430.    if (!equal(str,"")) {
  431.       if (!writestr(str)) {
  432.          slowwrite("\r", 1);
  433.       }
  434.    } else
  435.       slowwrite("\r", 1);
  436.    return;
  437.  
  438. } /*sendstr*/
  439.